package modules::utilities;
use strict;
use Data::Dumper;
use v5.10;
sub new {
  my ($class) = @_;
  my $self = {
               CONFIG_FILE_PATH => '',
             };
  bless $self,$class;
  return $self;
}

sub read_conf_file {
  my ($self,$conf,$conf_file_type) = @_;
  $self->{'CONFIG_FILE_PATH'}      = $conf;
  my ($user_prefs,$samples);
  
  open CONFIG,$conf or die $!;
  while (<CONFIG>) {
    chomp;                  # no newline
    s/#.*//;                # no comments
    s/^\s+//;               # no leading white
    s/\s+$//;               # no trailing white
    next unless length;     # anything left?
    my ($param, $value) = split(/\s*=\s*/, $_, 2);
    
    $value = '' if !defined $value;
    $user_prefs->{$param} = $value;
  } 
  close(CONFIG);
  
  # only perform this operation once
  if ($conf_file_type eq 'file_paths') {
    $self->create_sample_prefs( $user_prefs ) if ($conf_file_type eq 'file_paths') ;
    $self->check_files_exist( $user_prefs );
  }

  return $user_prefs;
}

sub create_sample_prefs {
  my ($self,$sample_prefs) = @_;
  my ($samples,$conditions);
  my $groups         = [ split( /;/,$sample_prefs->{'SAMPLE_GROUPS'} ) ]; # array of sample groups as splitted by ';'
  map { push @$samples,[ split( /,/,$_ ) ] } @$groups; # array of samples whose elements contain an array of replicates
  $conditions        = [ split( /;/,$sample_prefs->{'COND'} ) ];
  $sample_prefs->{'NO_SAMPLES'     } = scalar @$groups;
  $sample_prefs->{'NO_REPLICATES'  } = scalar @{$samples->[0]};

  
  $sample_prefs->{'_tophat_dir'     } = "$sample_prefs->{'PATH'}/results/$sample_prefs->{'JOB_NAME'}/TopHat/";
  $sample_prefs->{'_cufflinks_dir'  } = "$sample_prefs->{'PATH'}/results/$sample_prefs->{'JOB_NAME'}/Cufflinks/";
  $sample_prefs->{'_cuffmerge_dir'  } = "$sample_prefs->{'PATH'}/results/$sample_prefs->{'JOB_NAME'}/Cuffmerge/";
  $sample_prefs->{'_cuffdiff_dir'   } = "$sample_prefs->{'PATH'}/results/$sample_prefs->{'JOB_NAME'}/Cuffdiff/";
  $sample_prefs->{'_cuffdiff_recursive_dir'   } = "$sample_prefs->{'PATH'}/results/$sample_prefs->{'JOB_NAME'}/Cuffdiff_recursive/";
  $sample_prefs->{'_cuffcompare_dir'} = "$sample_prefs->{'PATH'}/results/$sample_prefs->{'JOB_NAME'}/Cuffcompare/";
  
  $sample_prefs->{'_job_name'}       = $sample_prefs->{'JOB_NAME'};
  $sample_prefs->{'_path'}           = $sample_prefs->{'PATH'};
  $sample_prefs->{'_genome'}         = $sample_prefs->{'GENOME'};
  $sample_prefs->{'_gtf_file'}       = $sample_prefs->{'GTF_FILE'};
  $sample_prefs->{'_fastq_dir'}      = $sample_prefs->{'FASTQ_DIR'};
  $sample_prefs->{'_samples' }       = $samples;
  $sample_prefs->{'_cond' }          = $conditions;  
  return;
}

sub check_files_exist {
  my ($self,$conf) = @_;
  my $fastq_dir = $conf->{'FASTQ_DIR'};

  foreach my $sample (@{$conf->{'SAMPLES'}}) {
    foreach my $replica (@$sample) {
      my $filename = $fastq_dir . '/' . $replica;
      die "$filename --> $!" unless (-e $filename) ;
    }
  }
  return;
}
sub check_completion {
  my ($self,$look_for) = @_;
  my $wait = 1;
  while ($wait == 1) {
    my  @info = qx(bjobs);
    print scalar localtime() . "\n";
    if(my @jobs = grep(/$look_for/, @info) ) { map{print $_}@jobs; sleep 10 }
    else {$wait = 0}  
  }
  return;
}
1;